package com.ciquan.server.controller;

import com.ciquan.server.bean.PushMessage;
import com.ciquan.server.bean.SysMessage;
import com.ciquan.server.service.PhoneCodeService;
import com.ciquan.server.service.PushMessageService;
import com.ciquan.server.service.SysMessageService;
import com.ciquan.server.util.DateUtils;
import com.ciquan.server.util.web.ParameterMap;
import com.ciquan.server.util.web.Result;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by bjliwei2014 on 2015/3/10.
 */
@Controller
@RequestMapping("/sysMessage")
public class SysMessageController extends AbstractController {

    @Resource
    private SysMessageService sysMessageService;

    @Resource
    private PushMessageService pushMessageService;

    @Resource
    private PhoneCodeService phoneCodeService;

    @RequestMapping("/list/page{page}")
    public ModelAndView list(HttpServletRequest request, @PathVariable String page, ModelMap modelMap) {
        int pageNum = initPage(page);
        ParameterMap parameterMap = new ParameterMap();
        parameterTrimInit(request, parameterMap);

        int count = sysMessageService.selectCount();
        page(count, pageNum, "sysMessage/list/page", parameterMap, request, modelMap);

        List<SysMessage> sysMessages = sysMessageService.selectList(parameterMap);
        for(SysMessage sysMessage : sysMessages) {
            wrapSysMessage(sysMessage);
        }

        modelMap.put("sysMessages", sysMessages);
        return new ModelAndView("sysMessageList", modelMap);
    }

    @RequestMapping("/add")
    public ModelAndView add() {
        return new ModelAndView("addSysMessage");
    }

    @RequestMapping("/saveAdd")
    public ModelAndView saveAdd(HttpServletRequest request) {
        String title = ServletRequestUtils.getStringParameter(request, "title", "");
        String content = ServletRequestUtils.getStringParameter(request, "content", "");
        String url = ServletRequestUtils.getStringParameter(request, "url", "");

        SysMessage sysMessage = new SysMessage();
        sysMessage.setTitle(title);
        sysMessage.setContent(content);
        sysMessage.setUrl(url);
        sysMessageService.insert(sysMessage);

        PushMessage pushMessage = new PushMessage();
        pushMessage.setTitle(title);
        pushMessage.setContent(content);
        pushMessage.setType("3");
        pushMessageService.sendBroadcastMessage(pushMessage);
        return new ModelAndView("redirect:/sysMessage/list/page1");
    }

    @RequestMapping("/delete")
    public ModelAndView delete(HttpServletRequest request) {
        Integer id = ServletRequestUtils.getIntParameter(request, "id", 0);
        sysMessageService.delete(id);
        return new ModelAndView("redirect:/sysMessage/list/page1");
    }

    @RequestMapping(value = "/sendAll", produces = "text/plain;charset=UTF-8", method = {RequestMethod.POST})
    @ResponseBody
    public String sendAll() {
        Result result = new Result();
        result.setSuccess(true);
        new Thread(new Runnable() {
            @Override
            public void run() {
                phoneCodeService.sendAll();
            }
        }).start();
        return result.toString();
    }

    private void wrapSysMessage(SysMessage sysMessage) {
        sysMessage.setCreateTimeStr(DateUtils.formatToString(sysMessage.getCreateTime(), DateUtils.yyyyMMddHHmmss));
    }
}
